﻿@page "/referenceassembly/edit/{ReferenceAssemblyId:int}"
@attribute [Authorize(Roles = "User, Administrator")]
@inherits OwningComponentBase<ICovenantService>

@using Covenant.Core
@using Covenant.Models.Grunts
@inject NavigationManager NavigationManager

<div class="d-flex justify-content-between flex-wrap flex-md-nowrap pb-2 mb-3">
    <h1 class="h2">ReferenceAssembly: <span class="primary-color">@ReferenceAssembly.Name</span></h1>
</div>

<ReferenceAssemblyForm ReferenceAssembly="ReferenceAssembly" OnSubmit="OnEdit" SubmitIcon="edit" SubmitLabel="Edit" />

@code {
    [Parameter]
    public int ReferenceAssemblyId { get; set; }

    [Parameter]
    public ReferenceAssembly ReferenceAssembly { get; set; }

    protected override async Task OnInitializedAsync()
    {
        this.ReferenceAssembly = this.ReferenceAssembly ?? await Service.GetReferenceAssembly(this.ReferenceAssemblyId);
    }

    public async Task OnEdit(ReferenceAssembly assembly)
    {
        try
        {
            await Service.EditReferenceAssembly(assembly);
            NavigationManager.NavigateTo("/referenceassembly");
        }
        catch (Exception e) when (e is ControllerNotFoundException || e is ControllerBadRequestException || e is ControllerUnauthorizedException)
        {
            // return RedirectToAction(nameof(Edit), new { id = assembly.Id });
        }
    }
}
